package com.genius.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.genius.annotation.Log;
import com.genius.annotation.PreFuncAuth;
import com.genius.core.JSONResult;
import com.genius.enums.BusinessType;
import com.genius.service.MenuService;
import com.genius.system.bo.DeleteMenuByIdsBO;
import com.genius.system.bo.InsertMenuBO;
import com.genius.system.bo.SelectMenuBO;
import com.genius.system.bo.UpdateMenuBO;
import com.genius.system.pojo.Menu;
import com.genius.system.pojo.User;
import com.genius.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
@Api(value = "菜单管理接口", tags = {"用于菜单管理的接口"})
public class MenuController {

    @Autowired
    private MenuService menuService;

    @PostMapping("/selectAllMenu")
    @PreFuncAuth("SYS:MENU:SELECT")
    @ApiOperation(value = "查询菜单(所有菜单)",notes = "查询所有菜单", httpMethod = "POST")
    public JSONResult selectAllMenu(@Valid @RequestBody SelectMenuBO selectMenuBO) {
        List<Menu> list;
        User user = SecurityUtils.getUser();
        if(user.getIsRoot()){
            Menu menu = JSONUtil.parse(selectMenuBO).toBean(Menu.class);
            list = menuService.selectAllMenu(menu);
        }else{
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("userId",user.getUserId());
            hashMap.put("menuName",selectMenuBO.getMenuName());
            hashMap.put("status",selectMenuBO.getStatus());
            list = menuService.selectMenuByUserId(hashMap);
        }
        return JSONResult.ok(list);
    }

    @PostMapping("/insertMenu")
    @ApiOperation(value = "添加菜单",notes = "添加菜单", httpMethod = "POST")
    @PreFuncAuth("SYS:MENU:INSERT")
    public JSONResult insertMenu(@Valid @RequestBody InsertMenuBO insertMenuBO) {
        Menu menu = JSONUtil.parse(insertMenuBO).toBean(Menu.class);
        Long menuId = IdUtil.getSnowflake().nextId();
        menu.setMenuId(menuId);
        menu.setCreateBy(SecurityUtils.getUsername());
        int rows = menuService.insertMenu(menu);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/updateMenu")
    @ApiOperation(value = "修改菜单",notes = "修改菜单", httpMethod = "POST")
    @PreFuncAuth("SYS:MENU:UPDATE")
    public JSONResult updateMenu(@Valid @RequestBody UpdateMenuBO updateMenuBO) {
        Menu menu = JSONUtil.parse(updateMenuBO).toBean(Menu.class);
        menu.setUpdateBy(SecurityUtils.getUsername());
        int rows = menuService.updateMenu(menu);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/deleteMenuByIds")
    @ApiOperation(value = "删除菜单",notes = "删除菜单", httpMethod = "POST")
    @PreFuncAuth("SYS:MENU:DELETE")
    @Log(title = "删除菜单", businessType = BusinessType.DELETE)
    public JSONResult deleteMenuByIds(@Valid @RequestBody DeleteMenuByIdsBO deleteMenuByIdsBO) {
        int rows = menuService.deleteMenuByIds(deleteMenuByIdsBO.getMenuIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
